package com.demo.datastructure.binarySearch;

/**
 * @author ljr
 * @version 1.0
 * @description: 简单二分的实现
 * @date 2021/10/13 17:13
 */
public class BinarySearch12 {

    public static int bsearch(int[] nums, int target) {
        int min = 0;
        int max = nums.length-1;
        while(min <= max){
            int mid = min+(max-min >> 1);
            if(nums[mid] > target){
                max = mid-1;
            }else if(nums[mid] < target){
                min = mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5};
        //int[] nums = new int[]{4,5,6,7,0,1,2};
        int index = BinarySearch11.bsearch(nums,3);
        System.out.println(index);
    }
}
